﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using System.Web.Http.OData;
using System.Web.Http.OData.Routing;
using Kemo.Models;

namespace Kemo.Controllers
{
    public class CustomerController : ODataController
    {
        private KemoContext db = new KemoContext();

        // GET odata/Customer
        [Queryable]
        public IQueryable<CustomerModel> GetCustomer()
        {
            return db.CustomerModels;
        }

        // GET odata/Customer(5)
        [Queryable]
        public SingleResult<CustomerModel> GetCustomerModel([FromODataUri] Guid key)
        {
            return SingleResult.Create(db.CustomerModels.Where(customermodel => customermodel.Id == key));
        }

        // PUT odata/Customer(5)
        public async Task<IHttpActionResult> Put([FromODataUri] Guid key, CustomerModel customermodel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (key != customermodel.Id)
            {
                return BadRequest();
            }

            db.Entry(customermodel).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerModelExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(customermodel);
        }

        // POST odata/Customer
        public async Task<IHttpActionResult> Post(CustomerModel customermodel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.CustomerModels.Add(customermodel);
            await db.SaveChangesAsync();

            return Created(customermodel);
        }

        // PATCH odata/Customer(5)
        [AcceptVerbs("PATCH", "MERGE")]
        public async Task<IHttpActionResult> Patch([FromODataUri] Guid key, Delta<CustomerModel> patch)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            CustomerModel customermodel = await db.CustomerModels.FindAsync(key);
            if (customermodel == null)
            {
                return NotFound();
            }

            patch.Patch(customermodel);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerModelExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(customermodel);
        }

        // DELETE odata/Customer(5)
        public async Task<IHttpActionResult> Delete([FromODataUri] Guid key)
        {
            CustomerModel customermodel = await db.CustomerModels.FindAsync(key);
            if (customermodel == null)
            {
                return NotFound();
            }

            db.CustomerModels.Remove(customermodel);
            await db.SaveChangesAsync();

            return StatusCode(HttpStatusCode.NoContent);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool CustomerModelExists(Guid key)
        {
            return db.CustomerModels.Count(e => e.Id == key) > 0;
        }
    }
}
